package de.fhd.pms.ctrl;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import de.fhd.pms.dao.KundeDao;
import de.fhd.pms.dao.DeleteException;
import de.fhd.pms.model.Kunde;
import de.fhd.pms.model.Fahrt;

/**
 * The Controller class for {@link Kunde} objects. Its tasks:
 * <ul>
 * <li>read and verify the parameters</li>
 * <li>access to the objects of the system</li>
 * <li>selection of the view and provision of content for the view</li>
 * </ul>
 * 
 * @author Josef Fischer
 * @author Alexander Neetzel
 * @author Philipp Six
 * 
 */
@Controller
public class KundeController {
	private static Logger log = Logger.getLogger(KundeController.class);
	private KundeDao kundeDao = null;

	public void setKundeDao(KundeDao kundeDao) {
		this.kundeDao = kundeDao;
	}

	/**
	 * <p>
	 * Set a {@link Kunde} object.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/SetKunde.html'.
	 * </p>
	 * 
	 * @param id
	 * @return View name 'kunde-set-kunde' selecting view
	 *         '/view/kunde-set-kunde.jsp'
	 */
	@RequestMapping(value = "/SetKunde.html", method = RequestMethod.GET)
	public ModelAndView set(@RequestParam(required = false) Integer id) {
		log.debug("SetKunde.html");
		ModelAndView mv = new ModelAndView();
		if (id == null) {
			mv.addObject(new Kunde());
		} else {
			mv.addObject(kundeDao.findById(id));
		}
		mv.setViewName("kunde-set-kunde");
		return mv;
	}

	/**
	 * <p>
	 * Edit a {@link Kunde} object.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/EditKunde.html'.
	 * </p>
	 * 
	 * @param id
	 * @return View name 'admin-edit-kunde' selecting view
	 *         '/view/admin-edit-kunde.jsp'
	 */
	@RequestMapping(value = "/EditKunde.html", method = RequestMethod.GET)
	public ModelAndView edit(@RequestParam(required = false) Integer id) {
		log.debug("EditKunde.html");
		ModelAndView mv = new ModelAndView();
		if (id == null) {
			mv.addObject(new Kunde());
		} else {
			mv.addObject(kundeDao.findById(id));
		}
		mv.setViewName("admin-edit-kunde");
		return mv;
	}

	/**
	 * <p>
	 * Saves a {@link Kunde} object.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP POST and request '/SaveKunde.html'.
	 * </p>
	 * 
	 * @param kunde
	 * @param model
	 * @return Redirect to '/SuccessKunde.html'
	 */
	@RequestMapping(value = "/SaveKunde.html", method = RequestMethod.POST)
	public String saveKunde(Kunde kunde, Model model) {
		if (kunde.getCreated() == null) {
			kunde.setCreated(new Date());
		}
		Kunde result = kundeDao.save(kunde);
		if (kunde.getId() == null) {
			kunde.setId(result.getId());
		}
		model.addAttribute("statusMessageKey", "kunde.form.msg.success");
		return "redirect:SuccessKunde.html";
	}

	/**
	 * <p>
	 * Saves a {@link Kunde} object.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP POST and request '/AdminSaveKunde.html'.
	 * </p>
	 * 
	 * @param kunde
	 * @param model
	 * @return Redirect to '/AllKunde.html'
	 */
	@RequestMapping(value = "/AdminSaveKunde.html", method = RequestMethod.POST)
	public String saveAdmin(Kunde kunde, Model model) {
		if (kunde.getCreated() == null) {
			kunde.setCreated(new Date());
		}
		Kunde result = kundeDao.save(kunde);
		if (kunde.getId() == null) {
			kunde.setId(result.getId());
		}
		model.addAttribute("statusMessageKey", "kunde.form.msg.success");
		return "redirect:AllKunde.html";
	}

	/**
	 * <p>
	 * Deletes a {@link Kunde} object.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/DeleteKunde.html'.
	 * </p>
	 * 
	 * @param kunde
	 * @return Redirect to '/AllKunde.html'.
	 * @throws DeleteException
	 *             if the specific {@link Kunde} object was in a {@link Fahrt}.
	 */
	@RequestMapping(value = "/DeleteKunde.html", method = RequestMethod.GET)
	public ModelAndView delete(Kunde kunde) {
		try {
			kundeDao.delete(kunde);
			return new ModelAndView("redirect:AllKunde.html");
		} catch (DeleteException e) {
			ModelAndView mv = new ModelAndView("admin-error");
			mv.addObject("message", e.getMessage());
			return mv;
		}
	}

	/**
	 * <p>
	 * Searches for all {@link Kunde} objects and returns them in a
	 * <code>Collection</code>.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/AllKunde.html'.
	 * </p>
	 * 
	 * @return View name 'admin-list-kunde' selecting view
	 *         '/view/admin-list-kunde.jsp'
	 */
	@RequestMapping(value = "/AllKunde.html", method = RequestMethod.GET)
	public ModelAndView findAll() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Kunde bearbeiten");
		mv.addObject("message", "Alle angelegten Kunden");
		mv.addObject("kunden", kundeDao.findAll());
		mv.setViewName("admin-list-kunde");
		return mv;
	}

	/**
	 * Gives the customer the information, that the {@link Kunde} object was set
	 * successfully.
	 * 
	 * <p>
	 * Expected HTTP GET and request '/SuccessKunde.html'.
	 * </p>
	 * 
	 * @return View name 'kunde-set-kunde-success' selecting view
	 *         '/view/kunde-set-kunde-success.jsp'
	 */
	@RequestMapping(value = "/SuccessKunde.html", method = RequestMethod.GET)
	public ModelAndView success() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Erfolgreich!");
		mv.addObject("message", "Ihre Kundendaten wurden erfolgreich angelegt.");
		mv.setViewName("kunde-set-kunde-success");
		return mv;
	}

}
